package bank.mvc;

import bank.command.AddInterestCommand;
import framework.account.IAccount;
import framework.command.CommandManager;
import framework.command.ICommand;
import framework.command.IFunctor;
import framework.mvc.ACustomButton;
import framework.mvc.AFincoView;
import framework.mvc.Finco;
import framework.mvc.FincoContext;

public class AddInterestButton extends ACustomButton {

    public AddInterestButton(AFincoView view) {
        super(view, "Add Interest");
    }

    @Override
    public void fireClick() {
        Finco model = FincoContext.getFinco();
        ICommand command = new AddInterestCommand(model.getAccounts());
        CommandManager.getInstance().submit(command, new IFunctor() {
            @Override
            public void process(IAccount acc) {
                acc.addInterest();
            }
        });

        for (int i = 0; i < view.getViewModel().getRowCount(); i++) {
            String accNumber = view.getViewModel().getValueAt(i, 0).toString();
            view.getViewModel().setValueAt(model.getAccount(accNumber).getBalance(), i, 5);
        }
        model.refresh();
    }
}
